package com.spirng.leetcode.day07;

import java.util.HashMap;
import java.util.Map;

public class Test654 {
    public static void main(String[] args) {
        Test654 test654 = new Test654();
        test654.constructMaximumBinaryTree(new int[]{3,2,1,6,0,5});
    }
    public TreeNode constructMaximumBinaryTree(int[] nums) {
      return findNode(nums,0, nums.length);
    }
    public TreeNode findNode(int[] nums,int Begin,int End){
        int Max=0;
        int  i=Begin;
        int rootNode = 0;
        if(Begin>=End){
            return null;
        }
        for ( ; i <End; i++) {
            if(Max<=nums[i]){
                Max=nums[i];
               rootNode=i;
            }
        }

        TreeNode treeNode = new TreeNode(Max);
        treeNode.left=findNode(nums,Begin,rootNode);
        treeNode.right=findNode(nums,rootNode+1,End);
        return treeNode;
    }
}
